from selenium import webdriver
import unittest
import time

class search(unittest.TestCase):
    # 测试固件
    def setUp(self):
        print("------setUp------")
        self.driver = webdriver.Chrome()
        self.url = "http://127.0.0.1:8080/"
        self.driver.maximize_window()
        time.sleep(3)

    def tearDown(self):
        print("------tearDown------")
        self.driver.quit()

    # 搜索为空
    def test_search1(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys()
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(5)
        self.assertEqual("蜡笔搜索", driver.title, msg="搜索失败")
        time.sleep(3)

    # 搜索内容为空格
    def test_search2(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys(" ")
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(5)
        self.assertEqual("蜡笔搜索", driver.title, msg="搜索失败")
        time.sleep(3)

    # 搜索一个关键字
    def test_search3(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys("string")
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(3)
        # 新窗口的定位
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        self.assertEqual("string - 蜡笔搜索", driver.title, msg="搜索成功")
        time.sleep(3)

    # 搜索一个关键字（文件中不存在）
    def test_search4(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys("我")
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(3)
        # 新窗口的定位
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        self.assertEqual("Whitelabel Error Page", driver.find_element_by_xpath("/html/body/h1").text, msg="搜索失败")
        time.sleep(3)

    # 搜索数字
    def test_search5(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys("2")
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(3)
        # 新窗口的定位
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        self.assertEqual("2 - 蜡笔搜索", driver.title, msg="搜索成功")
        time.sleep(3)

    # 搜索内容后加空格
    def test_search6(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys("string ")
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(3)
        # 新窗口的定位
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        self.assertEqual("string - 蜡笔搜索", driver.title, msg="搜索成功")
        time.sleep(3)

    # 多个关键字中间加入空格
    def test_search7(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys("list string")
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(3)
        # 新窗口的定位
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        self.assertEqual("list string - 蜡笔搜索", driver.title, msg="搜索成功")
        time.sleep(3)

    # 多个关键字中间加入逗号
    def test_search8(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys("list，string")
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(3)
        # 新窗口的定位
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        self.assertEqual("list，string - 蜡笔搜索", driver.title, msg="搜索成功")
        time.sleep(3)

    # 多个关键词搜索间隔多个空格
    def test_search9(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys("list   string")
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(3)
        # 新窗口的定位
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        self.assertEqual("list string - 蜡笔搜索", driver.title, msg="搜索成功")
        time.sleep(3)

    # 多个关键词搜索间隔多个逗号
    def test_search10(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys("list，，，string")
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(3)
        # 新窗口的定位
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        self.assertEqual("list，，，string - 蜡笔搜索", driver.title, msg="搜索成功")
        time.sleep(3)

    # 输入英文和数字的混合
    def test_search11(self):
        driver = self.driver
        url = self.url
        driver.get(url)
        driver.find_element_by_xpath("/html/body/div[1]/div/input").click()
        driver.find_element_by_xpath("/html/body/div[1]/div/input").send_keys("list3")
        driver.find_element_by_xpath("/html/body/div[1]/i[2]").click()
        time.sleep(3)
        # 新窗口的定位
        windows = driver.window_handles
        driver.switch_to.window(windows[1])
        self.assertEqual("list3 - 蜡笔搜索", driver.title, msg="搜索成功")
        time.sleep(3)